package xyz.liut.onmyojihelper

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.SwitchCompat
import android.view.LayoutInflater
import android.view.View
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import eu.chainfire.libsuperuser.Shell

/**
 * OnmyojiHelper
 * </p>
 * Created by liut on 2017/10/31 0031.
 */
class MainActivity : AppCompatActivity(), CompoundButton.OnCheckedChangeListener, View.OnClickListener {


    companion object {
        val TAG = "MainActivity"
    }

    private lateinit var roundNumTv: TextView
    private lateinit var roundTimeTv: TextView
    private lateinit var intervalTimeTv: TextView
    private var hasPermission: Boolean = true
    private var roundNum = 15
    private var roundTime = 70
    private var intervalTime = 5

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        roundNum = PreferenceManager
                .getDefaultSharedPreferences(this)
                .getInt("roundNum", 15)
        roundTime = PreferenceManager
                .getDefaultSharedPreferences(this)
                .getInt("roundTime", 70)
        intervalTime = PreferenceManager
                .getDefaultSharedPreferences(this)
                .getInt("intervalTime", 5)

        initView()

        checkRootPermission()
    }

    private fun checkRootPermission() {
        Thread(Runnable {
            Shell.SU.run("su")
        }).start()
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {
        findViewById<SwitchCompat>(R.id.sw_status).setOnCheckedChangeListener(this)
        findViewById<View>(R.id.config_ll).setOnClickListener(this)
        roundNumTv = findViewById(R.id.round_num_tv)
        roundTimeTv = findViewById(R.id.round_time_tv)
        intervalTimeTv = findViewById(R.id.interval_time_tv)
        roundNumTv.text = "总轮数: " + roundNum.toString()
        roundTimeTv.text = "单轮时间: " + roundTime.toString()
        intervalTimeTv.text = "间隔时间：" + intervalTime.toString()
    }

    @SuppressLint("InflateParams", "SetTextI18n")
    override fun onClick(p0: View?) {
        val alertView: View = LayoutInflater.from(this).inflate(R.layout.alert_config, null)
        val roundNumEt = alertView.findViewById<EditText>(R.id.round_num_et)
        val roundTimeEt = alertView.findViewById<EditText>(R.id.round_time_et)
        val intervalTimeEt = alertView.findViewById<EditText>(R.id.interval_time_et)
        roundNumEt.setText(roundNum.toString())
        roundTimeEt.setText(roundTime.toString())
        AlertDialog.Builder(this)
                .setView(alertView)
                .setTitle("配置")
                .setPositiveButton("确定") { _, _ ->
                    roundNum = roundNumEt.text.toString().toInt()
                    roundTime = roundTimeEt.text.toString().toInt()
                    intervalTime = intervalTimeEt.text.toString().toInt()
                    PreferenceManager
                            .getDefaultSharedPreferences(this@MainActivity)
                            .edit()
                            .putInt("roundNum", roundNum)
                            .putInt("roundTime", roundTime)
                            .putInt("intervalTime", intervalTime)
                            .apply()
                    roundNumTv.text = "总轮数: " + roundNum.toString()
                    roundTimeTv.text = "单轮时间: " + roundTime.toString()
                    intervalTimeTv.text = "间隔时间：" + intervalTime.toString()
                }
                .show()
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        val intent = Intent(this, CoreService::class.java)
        if (hasPermission) {
            if (isChecked && hasPermission) {
                intent.putExtra("roundNum", roundNum)
                intent.putExtra("roundTime", roundTime)
                intent.putExtra("intervalTime", intervalTime)
                startService(intent)
                Toast.makeText(this, "请打开阴阳师享用", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                stopService(intent)
                Toast.makeText(this, "服务已停止", Toast.LENGTH_SHORT).show()
            }
        } else {
            Toast.makeText(this, "请先授予悬浮窗权限", Toast.LENGTH_SHORT).show()
        }
    }

}